//
//  CTUserFollowView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/6/5.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTUserFollowView.h"
#import "CTDebugManager.h"
#import "CTHeader.h"
#import "CTUserFollowCell.h"
#import "CTAppLanguage.h"
#import "RefreshControl.h"
#import "UIViewAdditions.h"
#import "CTUserItem.h"
#import "CTUser.h"

@interface CTUserFollowView()<UITableViewDataSource,UITableViewDelegate,RefreshControlDelegate,CTUserFollowCellDelegate>
{
    UITableView     *followTableView;
    NSMutableArray  *followMutableArray;
    UIView          *topView;
    UILabel         *titleLabel;
    
    RefreshControl  *refreshControl;
}

@end

@implementation CTUserFollowView
@synthesize delegate;
- (void)dealloc
{
    delegate = nil;
    [followTableView removeFromSuperview];
    followTableView = nil;
    [topView removeFromSuperview];
    topView = nil;
    followMutableArray = nil;
    [titleLabel removeFromSuperview];
    titleLabel = nil;
    
    refreshControl  =nil;
    
    [CTDebugManager showErrorLogInfo:@"dealloc : CTUserFollowView Is Removed!!!!"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self createTitlView];
        [self createFanceTableView];
    }
    return self;
}
- (void)createTitlView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"userFollowTableView_title" withValue:nil withTable:@"Language"];
    titleLabel.font = [UIFont fontWithName:nil size:19];
    [topView addSubview:titleLabel];;
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    UILabel * topBottomLine = [[UILabel alloc]initWithFrame:CGRectMake(0, topView.height-0.5, CT_HEADER_SCREEN_WIDTH, 0.5)];
    topBottomLine.backgroundColor = CT_HEADER_DEEPLINE_COLOR;
    [topView addSubview:topBottomLine];
    
    
}
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(userFollowTableViewBackBtnAction:)])
    {
        [self.delegate userFollowTableViewBackBtnAction:self];
    }
}

- (void)createFanceTableView
{
    followTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    followTableView.delegate = self;
    followTableView.dataSource = self;
    followTableView.showsVerticalScrollIndicator = YES;
    followTableView.rowHeight = 68.0f;
    followTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    followTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    followTableView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [followTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self addSubview:followTableView];
    
    /*
     *** refresh control
     */
//    refreshControl = [[RefreshControl alloc] initWithScrollView:followTableView delegate:self];
//    refreshControl.topEnabled = YES;
//    refreshControl.bottomEnabled = NO;
//    refreshControl.enableInsetBottom = 60;
    
}
#pragma mark-UITableViewDataSource
//分区个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return followMutableArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    CTUserFollowCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[CTUserFollowCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.delegate = self;
    }
    cell.userItem = [followMutableArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
    CTUserItem *userItem = [followMutableArray objectAtIndex:indexPath.row];
    if ([self.delegate respondsToSelector:@selector(userFollowTableView:WithItme:)])
    {
        [self.delegate userFollowTableView:self WithItme:userItem];
    }
}



#pragma mark - action & delegate -
-(void)refresh
{
    [refreshControl startRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullDownEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionTop];
}

-(void)refreshPullUpEnd
{
    [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
}

-(void)reloadData
{
    [followTableView reloadData];
}


#pragma mark - refresh control delegate -

- (void)refreshControl:(RefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection) direction
{
    if (direction == RefreshDirectionTop) {
        if ([self.delegate respondsToSelector:@selector(refreshControlPullDown:)]) {
            [self.delegate refreshControlPullDown:self];
        }
        
    }else{
        if ([self.delegate respondsToSelector:@selector(refreshControlPullUp:)]) {
            [self.delegate refreshControlPullUp:self];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 0.0 && scrollView.contentOffset.y < 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionTop];
    }
    if (scrollView.contentOffset.y + scrollView.frame.size.height<= scrollView.contentSize.height && scrollView.contentOffset.y + scrollView.frame.size.height >= scrollView.contentSize.height - 80) {
        [refreshControl finishRefreshingDirection:RefreshDirectionBottom];
    }
    
    if (scrollView.contentOffset.y + scrollView.frame.size.height <= scrollView.contentSize.height+50) {
        refreshControl.isPullDown = YES;
    }else{
        refreshControl.isPullDown = NO;
    }
    
}




#pragma mark userFollowCellDelegate
- (void)userFollowCellAttentionBtnAction:(CTUserFollowCell *) cell withItem:(CTUserItem *)item withBool:(BOOL)attenBool
{
    if ([self.delegate respondsToSelector:@selector(userFollowTableViewAttentionBtnAction:withItem:withBool:)])
    {
        [self.delegate userFollowTableViewAttentionBtnAction:self withItem:item withBool:attenBool];
    }
}





#pragma mark -setter-
- (void)setFollowArray:(NSArray *)followArray
{
//    if (followArray.count < 10) {
//        refreshControl.bottomEnabled = NO;
//    }else{
//        refreshControl.bottomEnabled = YES;
//    }
    
    followMutableArray = [followArray mutableCopy];
    [followTableView reloadData];
}

- (void)setFollowUserItem:(CTUserItem *)item
{
    if ([item.uid isEqualToString:[CTUser userInfo].uid])
    {
        titleLabel.text = [CTAppLanguage localizedStringForKey:@"userFollowTableView_title" withValue:nil withTable:@"Language"];
    }
    else
    {
        titleLabel.text = [CTAppLanguage localizedStringForKey:@"userFollowTableView_otherTitle" withValue:nil withTable:@"Language"];
    }
}
@end
